import { Controller, Get, Query } from '@nestjs/common'
import { EmailService } from './email.service'
import { ResultFormat } from '../../format/ResultFormat'
import { ApiOperation, ApiTags } from '@nestjs/swagger'
import { EmailCodeDto } from './dto/email-code.dto'
import { PhoneCodeDto } from './dto/phone-code.dto'
import { TrafficDto } from './dto/trafficDto'

@ApiTags('邮箱')
@Controller('email')
export class EmailController {
  constructor(private readonly emailService: EmailService) {}

  @Get('sendEmailCode')
  @ApiOperation({ summary: '发送邮箱验证码' })
  async sendEmailCode(@Query() emailCode: EmailCodeDto): Promise<ResultFormat> {
    try {
      await this.emailService.sendEmailCode(emailCode)
      return new ResultFormat(0, null, true, "发送成功")
    } catch (e) {
      return new ResultFormat(0, e, false, "发送失败")
    }
  }

  @Get('sendPhoneCode')
  @ApiOperation({ summary: '发送手机验证码' })
  async sendPhoneCode(@Query() phoneCode: PhoneCodeDto): Promise<ResultFormat> {
    try {
      const result = await this.emailService.sendPhoneCode(phoneCode)
      if (result.RespCode !== '0000') {
        return new ResultFormat(0, result, false, result.RespMsg || '发送失败')
      }
      return new ResultFormat(0, result, true, "发送成功")
    } catch (e) {
      return new ResultFormat(0, e, false, "发送失败")
    }
  }

  @Get('getTraffic')
  @ApiOperation({ summary: '获取流量' })
  async getTraffic(@Query() trafficCode: TrafficDto): Promise<ResultFormat> {
    try {
      const result = await this.emailService.getTraffic(trafficCode)
      if (result.respCode !== '0000') {
        return new ResultFormat(0, result, false, result.RespMsg || '获取流量失败')
      }
      return new ResultFormat(0, result, true, "获取流量成功")
    } catch (e) {
      return new ResultFormat(0, e, false, "发送失败")
    }
  }
}
